package sort;

import java.util.HashMap;
import java.util.Map;

public class SortContext {
    private static final Map<SortType, Sort> strategyMap = new HashMap<>();

    static {
        strategyMap.put(SortType.InsertSort, new InsertSort());
        strategyMap.put(SortType.SelectSort, new SelectSort());
        strategyMap.put(SortType.BubbleSort, new BubbleSort());
        strategyMap.put(SortType.ShellSort, new ShellSort());
        strategyMap.put(SortType.MergeSort, new MergeSort());
        strategyMap.put(SortType.QuickSort, new QuickSort());
        strategyMap.put(SortType.HeapSort, new HeapSort());
        strategyMap.put(SortType.CountSort, new CountSort());
    }

    public void doSort(int[] arr, boolean asc, SortType type) {
        if (type == null) {
            throw new IllegalArgumentException("未指定排序类型");
        }
        Sort strategy = strategyMap.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("未实现的排序类型");
        }
        strategy.sort(arr, asc);
    }
}
